<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Http\Model\Goods;
use App\Http\Model\Type;
use App\Http\Model\Home_user;

use DB;

class MyclickController extends Controller
{
 public function pub2(){
        $link = \DB::table('sad_link')->orderBy('order','asc')->get();
        return $link;
    }
    /**
     * 我的点赞控制器
     *
     * 
     */
    public function getIndex(Request $request)
    {
        

        
        //获取用户的ID
        $uid = session('huser')->id;

        $user = Home_user::find($uid);
        $user['photo'] = \DB::table('sad_userinfo')->where('uid',session('huser')['id'])->get()[0]['photo'];
        $link = $this->pub2();
       //获取同个uid下的所有gid
        $gid = DB::table('sad_collection')->where('uid',$uid)->lists('gid');

        // dd($gid);

        //在商品表中的通过gid获取商品的详细信息
        $goods = new Goods();

        $mygoods = [];

        foreach($gid as $k=>$v)
        {

            $mygoods[] = $goods->where('id',$v)->get()->toArray();
            // dd($mygoods);
            
        }

        // dd($mygoods);
        $count = count($mygoods);
        // dd($count);
        //设置我的点赞的页面
        return view('home.user.myclick',compact('mygoods','count','user','link'));
    }


   
    //取消收藏的方法
    public function getDelete(Request $request)
    {

        //在session中获取用户的id
        $uid = session('huser')->id;

        //获取gid,
        $gid = $request->only('gid');

        // dd($gid);
       
        $collect = DB::table('sad_collection')->where('uid',$uid)->where('gid',$gid)->first();

        $status = $collect['status'];

        if($status==1)
        {

            $res = DB::table('sad_collection')->where('uid',$uid)->where('gid',$gid)->delete();
            if($res)
            {
                 $goods = new Goods();

                $temp = $goods->where('id',$gid)->first();

                // dd($temp);
                $count = $temp['count'];


                    if($count>0)
                    {

                        $count = $count-1;

                        $result = $goods->where('id',$gid)->update(['count'=>$count]);

                        if($result)
                        {

                            return back()->with('success','取消收藏成功');
                        }else
                        {
                            return back()->with('error','取消收藏失败');
                        }
                    }else
                    {
                        return back()->with('error','你好!你没有收藏该商品');
                    }

            }else
            {

                return back()->with('error','取消收藏失败');
            }
        }else
        {

            return back()->with('error','您未收藏此商品');
        }


    }

   
}
